package com.supplychainapi.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSourceStatLogger;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.supplychainapi.config.datasource.OrderDBConfig;

//@Configuration
//@MapperScan(basePackages = "com.supplychainapi.dao.order", sqlSessionTemplateRef = "orderSqlSessionTemplate")
public class OrderMyBatisConfig {

	@Bean(name = "orderDataSource")
	public DataSource orderDataSource(OrderDBConfig orderDBConfig) throws SQLException {

		DruidXADataSource druidXADataSource = new DruidXADataSource();
		druidXADataSource.setUrl(orderDBConfig.getUrl());
		druidXADataSource.setUsername(orderDBConfig.getUsername());
		druidXADataSource.setPassword(orderDBConfig.getPassword());
		druidXADataSource.setDriverClassName(orderDBConfig.getDriverClassName());
		druidXADataSource.setInitialSize(orderDBConfig.getInitialSize());
		druidXADataSource.setMinIdle(orderDBConfig.getMinIdle());
		druidXADataSource.setMaxActive(orderDBConfig.getMaxActive());
		druidXADataSource.setMaxWait(orderDBConfig.getMaxWait());
		druidXADataSource.setTimeBetweenEvictionRunsMillis(orderDBConfig.getTimeBetweenEvictionRunsMillis());
		druidXADataSource.setValidationQuery(orderDBConfig.getValidationQuery());
		druidXADataSource.setTestOnBorrow(orderDBConfig.isTestOnBorrow());
		druidXADataSource.setTestWhileIdle(orderDBConfig.isTestWhileIdle());
		druidXADataSource.setRemoveAbandoned(orderDBConfig.isRemoveAbandoned());
		druidXADataSource.setRemoveAbandonedTimeout(orderDBConfig.getRemoveAbandonedTimeout());
		
		String filters = orderDBConfig.getFilters();
		// 配置监控统计拦截的filters不为空，设置监控信息
		if(StringUtils.isNotBlank(filters)) {
			druidXADataSource.setFilters(orderDBConfig.getFilters());
			druidXADataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
			// 60秒一次，保存日志到log4j2中
			druidXADataSource.setTimeBetweenLogStatsMillis(60000);
			DruidDataSourceStatLogger dataSourceStatLogger = new DruidStatLogger();
			druidXADataSource.setStatLogger(dataSourceStatLogger);
		}
		
		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(druidXADataSource);
		xaDataSource.setUniqueResourceName("orderDataSource");
		xaDataSource.setMinPoolSize(orderDBConfig.getMinIdle());
		xaDataSource.setMaxPoolSize(orderDBConfig.getMaxActive());
		xaDataSource.setBorrowConnectionTimeout(60);
		xaDataSource.setMaxIdleTime(60);
		xaDataSource.setMaintenanceInterval(60);
		xaDataSource.setTestQuery(orderDBConfig.getValidationQuery());
		return xaDataSource;
	}

	@Bean(name = "orderSqlSessionFactory")
	public SqlSessionFactory orderSqlSessionFactory(@Qualifier("orderDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/order/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "orderSqlSessionTemplate")
	public SqlSessionTemplate orderSqlSessionTemplate(@Qualifier("orderSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
